package com.itheima.demo8test_gui;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class MovieService {
    // 4、准备一个集合容器：存储全部上架的电影数据。
    private static List<Movie> movies = new ArrayList<>();
    private JFrame frame;
    private JTextArea textArea;

    public void createAndShowGUI() {
        frame = new JFrame("电影信息操作系统");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(800, 600); // 调整窗口大小

        JPanel panel = new JPanel();
        panel.setLayout(new BorderLayout());
        panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10)); // 添加边距

        textArea = new JTextArea();
        textArea.setEditable(false);
        textArea.setFont(new Font("楷体", Font.PLAIN, 14));
        textArea.setBackground(Color.LIGHT_GRAY); // 更改背景颜色
        textArea.append("欢迎使用电影信息操作系统！\n");
        textArea.append("您可以使用以下按钮进行操作：\n");
        textArea.append("上架电影：添加新电影到系统中。\n");
        textArea.append("下架电影：从系统中移除电影。\n");
        textArea.append("查询电影：查找特定电影的信息。\n");
        textArea.append("封杀明星：移除所有包含特定明星的电影。\n");
        textArea.append("退出系统：关闭应用程序。\n");
        textArea.append("查询所有电影：列出系统中所有电影。\n");
        textArea.append("修改电影：更新现有电影的信息。\n");
        textArea.append("排序电影：根据评分或价格排序电影。\n");
        JScrollPane scrollPane = new JScrollPane(textArea);
        panel.add(scrollPane, BorderLayout.CENTER);

        JPanel buttonPanel = new JPanel();
        buttonPanel.setLayout(new GridBagLayout()); // 使用GridBagLayout布局
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(5, 5, 5, 5); // 设置间距

        // 上架电影
        JButton addButton = new JButton("上架电影");
        addButton.setFont(new Font("楷体", Font.PLAIN, 14));
        addButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                addMovie();
                JOptionPane.showMessageDialog(frame, "上架成功！");
            }
        });
        gbc.gridx = 0;
        gbc.gridy = 0;
        buttonPanel.add(addButton, gbc);

        // 查询所有电影
        JButton queryAllButton = new JButton("查询所有电影");
        queryAllButton.setFont(new Font("楷体", Font.PLAIN, 14));
        queryAllButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                queryAllMovies();
                JOptionPane.showMessageDialog(frame, "查询成功！");
            }
        });
        gbc.gridx = 1;
        gbc.gridy = 0;
        buttonPanel.add(queryAllButton, gbc);

        // 修改电影
        JButton updateButton = new JButton("修改电影");
        updateButton.setFont(new Font("楷体", Font.PLAIN, 14));
        updateButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String name = JOptionPane.showInputDialog(frame, "请输入要修改的电影名称：");
                Movie movie = queryMovieByName(name);
                if (movie != null) {
                    String newName = JOptionPane.showInputDialog(frame, "请输入新的电影名称：", movie.getName());
                    String newActor = JOptionPane.showInputDialog(frame, "请输入新的主演：", movie.getActor());
                    double newPrice = Double.parseDouble(JOptionPane.showInputDialog(frame, "请输入新的电影价格：", String.valueOf(movie.getPrice())));
                    double newScore = Double.parseDouble(JOptionPane.showInputDialog(frame, "请输入新的电影评分：", String.valueOf(movie.getScore())));

                    movie.setName(newName);
                    movie.setActor(newActor);
                    movie.setPrice(newPrice);
                    movie.setScore(newScore);
                    textArea.append("修改成功！\n");
                } else {
                    textArea.append("没有找到这个电影！\n");
                }
            }
        });
        gbc.gridx = 2;
        gbc.gridy = 0;
        buttonPanel.add(updateButton, gbc);

        // 下架电影
        JButton removeButton = new JButton("下架电影");
        removeButton.setFont(new Font("楷体", Font.PLAIN, 14));
        removeButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                removeMovie();
                JOptionPane.showMessageDialog(frame, "下架成功！");
            }
        });
        gbc.gridx = 0;
        gbc.gridy = 1;
        buttonPanel.add(removeButton, gbc);

        // 查询电影
        JButton queryButton = new JButton("查询电影");
        queryButton.setFont(new Font("楷体", Font.PLAIN, 14));
        queryButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                queryMovie();
                JOptionPane.showMessageDialog(frame, "查询成功！");
            }
        });
        gbc.gridx = 1;
        gbc.gridy = 1;
        buttonPanel.add(queryButton, gbc);

        // 封杀明星
        JButton deleteStarButton = new JButton("封杀明星");
        deleteStarButton.setFont(new Font("楷体", Font.PLAIN, 14));
        deleteStarButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                deleteStar();
                JOptionPane.showMessageDialog(frame, "封杀成功！");
            }
        });
        gbc.gridx = 2;
        gbc.gridy = 1;
        buttonPanel.add(deleteStarButton, gbc);

        // 排序电影
        JButton sortButton = new JButton("排序电影");
        sortButton.setFont(new Font("楷体", Font.PLAIN, 14));
        sortButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                sortMovies();
                JOptionPane.showMessageDialog(frame, "排序成功！");
            }
        });
        gbc.gridx = 0;
        gbc.gridy = 2;
        buttonPanel.add(sortButton, gbc);

        // 退出系统
        JButton exitButton = new JButton("退出系统");
        exitButton.setFont(new Font("楷体", Font.PLAIN, 14));
        exitButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
        gbc.gridx = 1;
        gbc.gridy = 2;
        buttonPanel.add(exitButton, gbc);

        panel.add(buttonPanel, BorderLayout.EAST);

        frame.add(panel);
        frame.setVisible(true);
    }

    private void queryAllMovies() {
        textArea.append("===========展示全部电影============\n");
        if (movies.isEmpty()) {
            textArea.append("当前没有电影！\n");
            return;
        }

        // 创建表格模型
        DefaultTableModel model = new DefaultTableModel();
        model.addColumn("电影名称");
        model.addColumn("主演");
        model.addColumn("价格");
        model.addColumn("评分");

        // 添加电影数据到表格模型
        for (Movie m : movies) {
            model.addRow(new Object[]{m.getName(), m.getActor(), m.getPrice(), m.getScore()});
        }

        // 创建表格并设置模型
        JTable table = new JTable(model);
        table.setFont(new Font("楷体", Font.PLAIN, 14));

        // 创建滚动面板并添加表格
        JScrollPane scrollPane = new JScrollPane(table);
        scrollPane.setPreferredSize(new Dimension(700, 300));

        // 创建对话框并添加滚动面板
        JDialog dialog = new JDialog(frame, "所有电影", true);
        dialog.add(scrollPane);
        dialog.pack();
        dialog.setLocationRelativeTo(frame);
        dialog.setVisible(true);
    }

    private void deleteStar() {
        textArea.append("===========封杀明星============\n");
        textArea.append("请您输入要封杀的明星：\n");
        String star = JOptionPane.showInputDialog(frame, "请输入要封杀的明星：");

        for (int i = 0; i < movies.size(); i++) {
            Movie movie = movies.get(i);
            if (movie.getActor().contains(star)) {
                movies.remove(movie);
                i--; // 退一步
            }
        }
        textArea.append("封杀成功！\n");
        queryAllMovies();
    }

    private void removeMovie() {
        textArea.append("===========下架电影============\n");
        textArea.append("请您输入电影名称：\n");
        String name = JOptionPane.showInputDialog(frame, "请输入电影名称：");

        Movie movie = queryMovieByName(name);
        if (movie != null) {
            movies.remove(movie);
            textArea.append("下架成功！\n");
        } else {
            textArea.append("没有找到这个电影！\n");
        }
        queryAllMovies();
    }

    private void queryMovie() {
        textArea.append("===========查询电影============\n");
        textArea.append("请您输入电影名称：\n");
        String name = JOptionPane.showInputDialog(frame, "请输入电影名称：");

        Movie movie = queryMovieByName(name);
        if (movie != null) {
            textArea.append("电影名称: " + movie.getName() + "\n");
            textArea.append("主演: " + movie.getActor() + "\n");
            textArea.append("价格: " + movie.getPrice() + "\n");
            textArea.append("评分: " + movie.getScore() + "\n");
        } else {
            textArea.append("没有找到这个电影！\n");
        }
    }

    private Movie queryMovieByName(String name) {
        for (Movie m : movies) {
            if (m.getName().equals(name)) {
                return m; // 找到这个电影对象
            }
        }
        return null; // 没有找到这个电影对象
    }

    private void addMovie() {
        textArea.append("===========上架电影============\n");

        String name = JOptionPane.showInputDialog(frame, "请输入电影名称：");
        String actor = JOptionPane.showInputDialog(frame, "请输入主演：");
        double price = Double.parseDouble(JOptionPane.showInputDialog(frame, "请输入电影价格："));
        double score = Double.parseDouble(JOptionPane.showInputDialog(frame, "请输入电影评分："));

        Movie movie = new Movie(name, score, actor, price);
        movies.add(movie);
        textArea.append("上架成功！\n");
    }

    // 新增排序功能
    private void sortMovies() {
        textArea.append("===========排序电影============\n");
        String criteria = JOptionPane.showInputDialog(frame, "请输入排序标准（评分/价格）：");

        if ("评分".equals(criteria)) {
            Collections.sort(movies, Comparator.comparingDouble(Movie::getScore).reversed());
        } else if ("价格".equals(criteria)) {
            Collections.sort(movies, Comparator.comparingDouble(Movie::getPrice));
        } else {
            textArea.append("无效的排序标准！\n");
            return;
        }

        textArea.append("排序成功！\n");
        queryAllMovies();
    }
}